package tests;

import static org.junit.Assert.*;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Properties;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import ownserver.ServerShip;
import pathfinding.DStarPathfinderSimple;
import pathfinding.GridCell;
import pathfinding.GridGeometry;
import pathfinding.SpaceTimeReservationTable;

import base.Timestamped;

import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.DataManager;
import com.sun.sgs.auth.Identity;
import com.sun.sgs.kernel.TransactionScheduler;
import com.sun.sgs.test.util.SgsTestNode;
import com.sun.sgs.test.util.TestAbstractKernelRunnable;

public class DStarPathfinderSimpleTest {

	private static SgsTestNode serverNode;
	
    private static TransactionScheduler txnScheduler;
    private static Identity taskOwner;
    
    private static final String NAME = "DStarPathfinderSimpleTest";
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		//serverNode = new SgsTestNode(NAME, null, null);
        
		Properties p = SgsTestNode.getDefaultProperties(NAME, null, SgsTestNode.DummyAppListener.class);
        
		p.setProperty("com.sun.sgs.txn.timeout", "4294967"); // for debugging
		
        serverNode = new SgsTestNode(NAME, null, p);
        
        txnScheduler = serverNode.getSystemRegistry().
                getComponent(TransactionScheduler.class);
        taskOwner = serverNode.getProxy().getCurrentOwner();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		serverNode.shutdown(true);
	}

	@Test
	public void testGetPath() throws Exception {
		txnScheduler.runTask(
				new TestAbstractKernelRunnable() {
				public void run() throws Exception {
					DataManager dataManager = AppContext.getDataManager();
					SpaceTimeReservationTable table = new SpaceTimeReservationTable();
					ServerShip s = new ServerShip(0,0,0,0,0, 0);
					DStarPathfinderSimple pathfinder = new DStarPathfinderSimple(dataManager.createReference(table), dataManager.createReference(s));
					
					ArrayList<Timestamped<GridCell>> path = pathfinder.getPath(new GridCell(0,0), new GridCell(1,2));
					assertTrue(path.size() == 3);
					// <0,0> <0,1> <1,2> 
					
					path = pathfinder.getPath(new GridCell(0,0), new GridCell(0,10));
					assertTrue(path.size() == 11);
					
					//System.out.println(pathfinder.getInnerDatastructures());
					
					ServerShip blocker = new ServerShip(1,0,0,0,0, 0);
					table.reserve(new GridCell(0,1), 0, Long.MAX_VALUE, dataManager.createReference(blocker));
					table.reserve(new GridCell(1,1), 0, Long.MAX_VALUE, dataManager.createReference(blocker));
					table.reserve(new GridCell(-1,1), 0, Long.MAX_VALUE, dataManager.createReference(blocker));
					
					//System.out.println(pathfinder.getInnerDatastructures());
					
					path = pathfinder.getPath(new GridCell(0,0), new GridCell(0,10));
					assertTrue(path.size() == 12);
					
					pathfinder = new DStarPathfinderSimple(dataManager.createReference(table), dataManager.createReference(s));
					path = pathfinder.getPath(new GridCell(0,0), new GridCell(0,10));
					assertTrue(path.size() == 12);
					
					table.removeReservations(dataManager.createReference(blocker), false);
					path = pathfinder.getPath(new GridCell(0,0), new GridCell(0,10));
					assertTrue(path.size() == 11);
					
					path = pathfinder.getPath(GridGeometry.getCell(s), new GridCell(1,2));
					assertTrue(path.size() == 3);
					// <0,0> <0,1> <1,2>
				}
				}, taskOwner);
	}

	@Test
	public void testNeedsUpdate() throws Exception {
		txnScheduler.runTask(
				new TestAbstractKernelRunnable() {
				public void run() throws Exception {
					
					
				}
				}, taskOwner);
	}

}
